const MusicHttp = require("../../utils/MusicHttp")

Page({

  data: {
    banner:[],
    hot:[],
    new:[],
    dj:[],
    currentCity:""
  },

  onLoad: async function (options) {
    var bannerRes = await MusicHttp.getBannerMusicHttp();
    // console.log(bannerRes.data.banners);
    var hotRes = await MusicHttp.getHotMusicHttp();
    // console.log(hotRes.data.playlists);
    var newRes = await MusicHttp.getNewMusicHttp();
    newRes.data.albums.slice(0,3).forEach(item=>{
      var {blurPicUrl} = item;
      item.coverImgUrl = blurPicUrl;
    })
    // console.log(newRes.data.albums.slice(0,3));
    var djRes = await MusicHttp.getDjMusicHttp();
    // console.log(djRes.data.result);
    djRes.data.result.slice(0,3).forEach(item=>{
      
      var {program} =item
      // console.log(program.listenerCount);
      item.playCount = program.listenerCount;
      item.coverImgUrl = item.picUrl
    })
    this.setData({
      banner:bannerRes.data.banners,
      hot:hotRes.data.playlists.slice(0,3),
      new:newRes.data.albums.slice(0,3),
      dj:djRes.data.result.slice(0,3)
    })
  },
  onClickCity(){
    wx.navigateTo({
      url: '../city/city',
    })
  },
  onSearchClick(){
    wx.navigateTo({
      url: '../search/search',
    })
  },
  Morehot(){
    var url = `3000/top/playlist/highquallity`
    wx.navigateTo({
      url: `../MoreMusic/MoreMusic?url=${url}`,
    })
    wx.setNavigationBarTitle({
      title: '热门歌曲',
    })
  },
  Morenew(){
    var url = `3000/album/newest`
    wx.navigateTo({
      url: `../MoreMusic/MoreMusic?url=${url}`,
    })
    wx.setNavigationBarTitle({
      title: '最新音乐',
    })
  },
  Moredj(){
    var url = `3000/personalized/djprogram`
    wx.navigateTo({
      url: `../MoreMusic/MoreMusic?url=${url}`,
    })
    wx.setNavigationBarTitle({
      title: '主播电台',
    })
  },
  onReady: function () {

  },

  onShow: function () {
    var city = wx.getStorageSync('city')
    this.setData({
      currentCity:city
    })
    wx.getLocation({
      altitude: 'altitude',
      highAccuracyExpireTime: 0,
      isHighAccuracy: true,
      type: 'type',
      success: (res) => {
        // console.log(res);
        var {latitude,longitude} =res;
        var key = "5SHBZ-N3D3I-M4IGS-5Y7DV-RQXEQ-SRBNV";
        var url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}&get_poi=1`;
        wx.request({
          url,
          header: {
            'content-type': 'application/json'
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: (res) => {
            // console.log(res);
            //比较当前所在城市和缓存中城市是否一样
            var currentCity = res.data.result.address_component.city;
            var storageCity = wx.getStorageSync('city')
            if(currentCity!=storageCity){
            //询问是否切换城市
              wx.showModal({
                title: '切换城市',
                content: '当时城市不是您所在的城市',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                  if(result.confirm){
                    this.setData({
                      currentCity
                    })
                    wx.setStorageSync('city', currentCity);
                  }else{
                    this.setData({
                      currentCity:storageCity,
                    })
                  }
                },
              });
            } 
          }
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})